<template>
  <div class="fruitManagement">
    <div style="width: 98%; margin: auto">
      <div class="header">
        <div>
          <el-button type="primary" @click="newlyDdded"> 新增 </el-button>
          <el-button>批量删除</el-button>
        </div>
        <div class="header-right">
          <el-input
            placeholder="请输入商品名称"
            prefix-icon="el-icon-search"
            v-model="searchinput"
            clearable
          ></el-input>
          <el-button type="primary" @click="tableLsit">查询</el-button>
        </div>
      </div>
      <el-table
        :data="tableData"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column type="index" label="序号"> </el-table-column>
        <el-table-column
          prop="goodsName"
          label="商品名称"
          show-overflow-tooltip
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="insideKey"
          label="详情介绍"
          show-overflow-tooltip
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="retailPrice"
          label="预售价格"
          show-overflow-tooltip
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="mallPrice"
          label="实际价格"
          show-overflow-tooltip
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="goodsBzq"
          label="规格"
          show-overflow-tooltip
          align="center"
        >
        </el-table-column>
        <el-table-column prop="address" label="图片" align="center">
          <template slot-scope="scope">
            <img :src="scope.row.goodsImg" alt="" style="width: 50px" />
          </template>
        </el-table-column>
        <el-table-column prop="address" label="产地" align="center">
        </el-table-column>
        <el-table-column prop="classification" label="分类" align="center">
        </el-table-column>
        <el-table-column prop="address" label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="addApproval(scope.row, scope.row.jobNo)"
              >编辑</el-button
            >
            <el-button type="text" size="small" @click="userdelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
    <el-dialog
      :title="title"
      :visible.sync="carInfoVisible"
      class="carInfo"
      width="40%"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="商品名称">
          <el-input v-model="form.goodsName"></el-input>
        </el-form-item>
        <el-form-item label="详情介绍">
          <el-input v-model="form.insideKey"></el-input>
        </el-form-item>
        <el-form-item label="预售价格">
          <el-input v-model="form.retailPrice"></el-input>
        </el-form-item>
        <el-form-item label="实际价格">
          <el-input v-model="form.mallPrice"></el-input>
        </el-form-item>
        <el-form-item label="规格">
          <el-input v-model="form.goodsBzq"></el-input>
        </el-form-item>
        <!-- <el-form-item label="图片">
          <el-upload
            class="avatar-uploader"
            action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="form.imageUrl" :src="form.imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item> -->
        <el-form-item label="产地">
          <el-input v-model="form.address"></el-input>
        </el-form-item>
        <el-form-item label="分类">
          <el-select v-model="form.classification" placeholder="请选择分类">
            <el-option label="热门水果" value="热门水果"></el-option>
            <el-option label="奇异果/车厘子" value="奇异果/车厘子"></el-option>
            <el-option
              label="苹果/梨子/瓜子"
              value="苹果/梨子/瓜子"
            ></el-option>
            <el-option
              label="提子/葡萄/霉类"
              value="提子/葡萄/霉类"
            ></el-option>

            <el-option label="橙桔柑柚" value="橙桔柑柚"></el-option>
            <el-option label="其他" value="其他"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">确认</el-button>
          <el-button @click="carInfoVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import {
  checkLikeList,
  saveOrUpdateMeager,
  delFruits,
} from "../common/Api/index";
export default {
  data() {
    return {
      searchinput: "",
      tableData: JSON.parse(sessionStorage.getItem("list")),
      carInfoVisible: false,
      form: {
        goodsName: "",
        insideKey: "",
        retailPrice: "",
        mallPrice: "",
        goodsBzq: "",
        address: "",
        imageUrl: "",
      },
      title: "",
      size: 1,
      limit: 10,
      total: null,
    };
  },
  activated() {},
  mounted() {
    this.tableLsit();
  },
  methods: {
    userdelete(item) {
      console.log(item);
      this.$confirm("此操作将永久用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delFruits(item.id).then((res) => {
            if (res == "删除成功") {
              this.tableLsit();
            }
            return;
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleCurrentChange(val) {
      this.size = val;
      this.tableLsit();
      console.log(val);
    },
    //列表查询
    tableLsit() {
      let arr = {
        key: this.searchinput,
        pagnum: this.size,
        limit: this.limit,
      };

      checkLikeList(arr).then((res) => {
        this.tableData = res.records;
        this.total = res.total;
        console.log(res);
      });
    },
    //新增或编辑
    onSubmit() {
      saveOrUpdateMeager(this.form).then((res) => {
        if (res == "添加成功") {
          this.$message({
            message: "添加成功",
            type: "success",
          });
          this.carInfoVisible = false;
          this.tableLsit();
        } else if (res == "修改成功") {
          this.$message({
            message: "修改成功",
            type: "success",
          });
          this.carInfoVisible = false;
          this.tableLsit();
        }
      });
    },
    handleSelectionChange() {},
    //新增
    newlyDdded() {
      this.carInfoVisible = true;
      this.title = "新增";
      this.form = {
        goodsName: "",
        insideKey: "",
        retailPrice: "",
        mallPrice: "",
        goodsBzq: "",
        address: "",
        imageUrl: "",
        classification: "",
      };
    },
    //编辑
    addApproval(item) {
      this.title = "编辑";
      this.form = {
        goodsName: item.goodsName,
        insideKey: item.insideKey,
        retailPrice: item.retailPrice,
        mallPrice: item.mallPrice,
        goodsBzq: item.goodsBzq,
        address: item.address,
        imageUrl: item.goodsImg,
        classification: item.classification,
        id: item.id,
      };
      this.carInfoVisible = true;
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>
<style lang="scss">
.fruitManagement {
  height: 100%;
  background: #fff;
  .header {
    display: flex;
    justify-content: space-between;
    padding: 20px 0;
    .header-right {
      width: 20%;
      .el-input {
        width: 70% !important;
      }
    }
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
}
</style>